package team.paimengshop.server_database;

import team.paimengshop.server_dataclass.Commodity;
import team.paimengshop.server_dataclass.Order;
import team.paimengshop.server_dataclass.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class OrderOperate
{
    private static Connection cn = PaiMengShopDatabase.getConnection();

    public OrderOperate()
    {

    }

    public static int Add(int userId, String address, String commodityList, int commodityNumber, double payment)
    {
        String sql = "insert into OrderInfo (userId, address, commodityList, commodityNumber, payment) values ('" + userId + "','" + address + "','"
                + commodityList + "'," + commodityNumber + "," + payment + ")";
        int n = -1;
        try
        {
            cn = PaiMengShopDatabase.getConnection();
            PreparedStatement ps = cn.prepareStatement(sql);
            n = ps.executeUpdate();
        } catch (SQLException e)
        {
            e.printStackTrace();
        }
        if (cn != null)
        {
            try
            {
                cn.close();
            } catch (SQLException e)
            {
                e.printStackTrace();
            }
        }
        return n;
    }

    public static int DeleteById(int id)
    {
        String sql = "delete from OrderInfo where id = ?";
        int n = -1;
        try
        {
            cn = PaiMengShopDatabase.getConnection();
            PreparedStatement ps = cn.prepareStatement(sql);
            ps.setString(1, Integer.toString(id));
            n = ps.executeUpdate();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        if (cn != null)
        {
            try
            {
                cn.close();
            } catch (SQLException e)
            {
                e.printStackTrace();
            }
        }
        return n;
    }

    public static Order FindById(int id)
    {
        Order order = new Order();
        String sql = "select * from OrderInfo where id = ?";
        try
        {
            cn = PaiMengShopDatabase.getConnection();
            PreparedStatement ps = cn.prepareStatement(sql);
            ps.setInt(1, id);
            ResultSet rs = ps.executeQuery();
            while(rs.next())
            {
                order.setId(rs.getInt(1));
                order.setUserId(rs.getInt(2));
                order.setAddress(rs.getString(3));
                String commodityList = rs.getString(4);
                order.setCommodityList(Commodity.getCommodityArrayListFromString(commodityList));
                order.setCommodityNumber(rs.getInt(5));
                order.setPayment(rs.getDouble(6));
            }
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        if (cn != null)
        {
            try
            {
                cn.close();
            } catch (SQLException e)
            {
                e.printStackTrace();
            }
        }
        return order;
    }

    public static ArrayList<Order> FindByUserId(int userId)
    {
        ArrayList<Order> orderArrayList = new ArrayList<>();
        String sql = "select * from OrderInfo where userId = ?";
        try
        {
            cn = PaiMengShopDatabase.getConnection();
            PreparedStatement ps = cn.prepareStatement(sql);
            ps.setInt(1, userId);
            ResultSet rs = ps.executeQuery();
            while(rs.next())
            {
                Order order = new Order();
                order.setId(rs.getInt(1));
                order.setUserId(rs.getInt(2));
                order.setAddress(rs.getString(3));
                String commodityList = rs.getString(4);
                order.setCommodityList(Commodity.getCommodityArrayListFromString(commodityList));
                order.setCommodityNumber(rs.getInt(5));
                order.setPayment(rs.getDouble(6));
                orderArrayList.add(order);
            }
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        if (cn != null)
        {
            try
            {
                cn.close();
            } catch (SQLException e)
            {
                e.printStackTrace();
            }
        }
        return orderArrayList;
    }

    public static ArrayList<Order> GetAllOrder()
    {
        ArrayList<Order> orderArrayList = new ArrayList<>();
        String sql = "select * from orderInfo";
        try
        {
            cn = PaiMengShopDatabase.getConnection();
            PreparedStatement ps = cn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while(rs.next())
            {
                Order order = new Order();
                order.setId(rs.getInt(1));
                order.setUserId(rs.getInt(2));
                order.setAddress(rs.getString(3));
                String commodityList = rs.getString(4);
                order.setCommodityList(Commodity.getCommodityArrayListFromString(commodityList));
                order.setCommodityNumber(rs.getInt(5));
                order.setPayment(rs.getDouble(6));
                orderArrayList.add(order);
            }
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        if (cn != null)
        {
            try
            {
                cn.close();
            } catch (SQLException e)
            {
                e.printStackTrace();
            }
        }
        return orderArrayList;
    }
}
